<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Token;
use app\common\model\Auto;
use app\common\model\card\Dealer;
use app\common\model\Member;
use app\common\model\Merchant;
use app\common\model\Merchantuser;
use app\common\model\Message as MessageModel;
use app\common\model\Regionserviceuser;

/**
 * 消息接口
 */
class Message extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    protected $regionserviceUser = [];
    protected $id = 0;
    protected $type = 0;//发送对象:0=用户,1=店铺,2=区域服务商,3=卡经销商,4=分销商

    public function _initialize()
    {
        parent::_initialize();
        if (!$this->auth->match($this->noNeedLogin)) {
            /*判断当前账号身份 start*/
            $token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('token')));
            $token = Token::get($token);
            //if ($token["type"] != 0) $this->error("身份错误");//1 店铺 2区域服务商 3卡经销商 4分销商 0普通用户
            /*判断当前账号身份 end*/
            $this->type = $token["type"];
            if (empty($token["type"])) {
                $user = Member::get($this->auth->id);
                if (empty($user)) $this->error("用户不存在");
                $this->id = $this->auth->id;
            }
            if ($token["type"] == 1) {
                $user = Merchantuser::get($this->auth->id);
                $merchant = Merchant::where(["id" => $user["merchant_id"]])->find();
                if (empty($merchant)) $this->error("店铺不存在");
                if (empty($merchant["status"])) $this->error("店铺审核中");
                if ($merchant["status"] == 2) $this->error("店铺已被驳回：" . $merchant["remark"]);
                if ($merchant["status"] == 3) $this->error("店铺已被禁用");
                $this->id = $merchant["id"];
            }
            if ($token["type"] == 2) {
                $user = Regionserviceuser::get($this->auth->id);
                $regionservice = \app\common\model\Regionservice::find($user["regionservice_id"]);
                if (empty($regionservice)) $this->error("区域服务商不存在");
                if (empty($regionservice["status"])) $this->error("区域服务商审核中");
                if ($regionservice["status"] == 2) $this->error("区域服务商已被驳回：" . $regionservice["remark"]);
                if ($regionservice["status"] == 3) $this->error("区域服务商已被禁用");
                $this->id = $regionservice["id"];
            }
            if ($token["type"] == 3) {
                $user = Dealer::get($this->auth->id);
                if (empty($user)) $this->error("卡经销商不存在");
                if (empty($user["status"])) $this->error("卡经销商审核中");
                if ($user["status"] == 2) $this->error("卡经销商已被驳回：" . $user["remark"]);
                if ($user["status"] == 3) $this->error("卡经销商已被禁用");
                $this->id = $user["id"];
            }
            if ($token["type"] == 4) {
                $user = \app\common\model\Distributor::get($this->auth->id);
                if (empty($user)) $this->error("渠道分销商不存在");
                if (empty($user["status"])) $this->error("渠道分销商审核中");
                if ($user["status"] == 2) $this->error("渠道分销商已被驳回：" . $user["remark"]);
                if ($user["status"] == 3) $this->error("渠道分销商已被禁用");
                $this->id = $user["id"];
            }

        }
    }

    /**
     * 消息列表
     * */
    public function list()
    {
        $page = !$this->request->param('page') ? 1 : $this->request->param('page');//页
        $list = MessageModel::field("id,title,from_unixtime(createtime, '%Y-%m-%d %H:%i:%s') createtime,is_read")
            ->where('FIND_IN_SET("' . $this->id . '", member_ids)')
            ->where(["sende_data" => $this->type])
            ->order("createtime", "desc")
            ->paginate(10, '', ['page' => $page, 'list_rows' => 10]);
        $this->success("成功", $list);
    }

    /**
     * 消息详情
     * */
    public function details()
    {
        if (!$this->request->param("id")) $this->error("消息id为空");
        $list = MessageModel::field("id,title,from_unixtime(createtime, '%Y-%m-%d %H:%i:%s') createtime,info,is_read")
            ->where(["id" => $this->request->param("id")])->find();
        if (empty($list)) $this->error("消息不存在");
        if ($list["is_read"] == 0) {
            $list->save(["is_read" => '1', "read_time" => time()]);
        }
        $this->success("成功", $list);
    }


    /**
     *查询是否有未读消息
     * */
    public function getUnread()
    {
        $list = MessageModel::field("1")
            ->where('FIND_IN_SET("' . $this->id . '", member_ids)')
            ->where(["sende_data" => $this->type,"is_read"=>'0'])->find();
        $read=false;
        if(!empty($list)) $read=true;
        $this->success("成功", $read);
    }
}
